const redux = require('redux')
const m1 = require('./m1')
const m2 = require('./m2')

const r1 = (state={count: 0}, action) => {
  switch(action.type) {
    case 'add':
      return {
        count: state.count + 1
      }
    default:
      return state
  }
}

const r2 = (state={count: 1}, action) => {
  switch(action.type) {
    case 'multi':
      return {
        count: state.count * 2
      }
    default:
      return state
  }
}

// r1, r2 所应用的函数要在 dispatch() 之后运行
const reducers = redux.combineReducers({
  r1,
  r2
})

const store = redux.createStore(
  reducers,
  // () => {},
  redux.applyMiddleware(m1, m2)
  // () => {}
  // void(0)
  // ['a']
  // createStore => {
  //   console.log(createStore)
  //   return (reducer, preloadedState) => {
  //     console.log(reducer)
  //     return 100
  //   }
  // }
)

// console.log(store)

// const action = store.dispatch({
//   type: ''
// })

const unsub = store.subscribe(() => {
  console.log(store.getState())
})

store.dispatch({
  type: 'add'
})

// unsub()

store.dispatch({
  type: 'add'
})
